package com.angenela.imageshare.util;

import com.angenela.imageshare.dto.TagDTO;
import com.angenela.imageshare.pojo.Tag;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class TagUtil {
    private static final String TAG_URL = "http://122.9.152.182:8080/tag/";

    private static List<Tag> data = null;

    static {
        try {
            OkHttpClient okHttpClient = new OkHttpClient();
            Request request = new Request.Builder()
                    .url(TAG_URL)
                    .build();
            Response response = okHttpClient.newCall(request).execute();

            ObjectMapper mapper = new ObjectMapper();
            TagDTO tagDTO = mapper.readValue(response.body().string(), TagDTO.class);
            data = tagDTO.getData();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static List<Tag> getData() {
        return data;
    }
}
